#define _CRT_SECURE_NO_WARNINGS 1
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x)
    {
        struct ListNode* greaterHead, * greaterTail = NULL;
        struct ListNode* LessHead, * LessTail = NULL;
        greaterHead = greaterTail = (ListNode*)malloc(sizeof(struct ListNode));
        LessHead = LessTail = (struct ListNode*)malloc(sizeof(struct ListNode*));
        greaterTail->next = LessTail->next = NULL;

        struct ListNode* cur = pHead;
        while (cur != NULL)
        {
            if (cur->val < x)
            {
                LessTail->next = cur;
                LessTail = LessTail->next;
            }
            else
            {
                greaterTail->next = cur;
                greaterTail = greaterTail->next;
            }
            cur = cur->next;
        }
        LessTail->next = greaterHead->next;
        greaterTail->next = NULL;
        struct ListNode* Phead = LessHead->next;
        free(greaterHead);
        free(LessHead);
        return Phead;
    }
};
